package com.wbc.smartHome.service;

import com.wbc.smartHome.dao.LoginDao;
import com.wbc.smartHome.model.Admin;
import com.wbc.smartHome.model.Menu;
import com.wbc.smartHome.util.JWTUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;

import java.util.List;

@Service
@Transactional(rollbackFor = Exception.class)
public class LoginService {
    @Autowired
    LoginDao loginDao;
    /*public List<Admin> login1() {
        List<Admin> admins = loginDao.login1();
        return admins;
    }*/

    public Admin login(Admin admin) {
        String password = admin.getPassword();
        admin.setPassword(DigestUtils.md5DigestAsHex(password.getBytes()));
        Admin admin1 = loginDao.login(admin);
        return admin1;
    }

    public List<Menu> menus(String adminToken) {
        Admin admin = JWTUtil.getAdmin(adminToken);
        int id = admin.getId();
        int type = admin.getType();
        List<Menu> menus = loginDao.menus(id,type);
        return menus;
    }
}
